﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using DisplayWord.Helper;
using System.Reflection;
using System.Runtime.InteropServices;

namespace DisplayWord
{
    public partial class Display : Form
    {
        private  const int HI_KA_COLUMN = 2;
        private const int KANJI_COLUMN = 3;
        private const int HV_COLUMN = 4;
        private const int VIETNAM_COLUMN = 5;
        private bool isKanji = true;
        private bool isVietnamese = true;

        private int noSecond = 1000;

        List<Words> list = null;

        int i = 0;
        public Display()
        {
            InitializeComponent();
            SetParam();
        }

        public Display(int noSecond)
        {
            InitializeComponent();

            this.noSecond = noSecond;
        }

        private List<Words> GetList()
        {

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            string str;
            int rCnt = 0;
            int cCnt = 0;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@"c:/NVGIOI/pj/DisplayWord/DisplayWord/Data/Bothu.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;

            for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            {
                //for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                //{
                //    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                //    MessageBox.Show(str);
                //}
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);


            //Excel.Application xlApp = new Excel.Application();
            //Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"c:/NVGIOI/pj/DisplayWord/DisplayWord/Data/Bothu.xlsx");
            //Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            //Excel.Range xlRange = xlWorksheet.UsedRange;

            //int rowCount = xlRange.Rows.Count;
            //int colCount = xlRange.Columns.Count;

            List<Words> listWords = new List<Words>();
            Words word = new Words();
            word.Hiragana = "";
            word.Kanji = "";
            word.VN_Chinese = "";
            word.Vietnamese = "";
            listWords.Add(word);

            //for (int i = 2; i <= rowCount; i++)
            //{
            //    Words word = new Words();
            //    word.Hiragana = xlRange.Cells[i, HI_KA_COLUMN].Value2;
            //    word.Kanji = xlRange.Cells[i, KANJI_COLUMN].Value2;
            //    word.VN_Chinese = xlRange.Cells[i, HV_COLUMN].Value2;
            //    word.Vietnamese = xlRange.Cells[i, VIETNAM_COLUMN].Value2;
            //    listWords.Add(word);
            //}

          

            return listWords;

        }


        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 


        private void SetParam()
        {
           // this.TransparencyKey = Color.Turquoise;
          //  this.BackColor = Color.Turquoise;
           // this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.ControlBox = false;
            this.Text = String.Empty;

            //timer
            timerChangeText.Interval = noSecond;

            lblHira.Visible = lblKanji.Visible = lblVN.Visible = lblHV.Visible = true;
            lblHira.Text = lblKanji.Text = lblVN.Text = "";

            //
           
            notifyIcon.ContextMenuStrip = contextMenuStrip1;
            this.ShowInTaskbar = false;
            
            //form location
           Position.SetPosition(this, Position.EnumPos.TopLeft);
        }

        private void timerChangeText_Tick(object sender, EventArgs e)
        {
            
            lblHira.Text = list[i].Hiragana;    

            //Kanji
            if (!string.IsNullOrEmpty(list[i].Kanji))
            {
                lblKanji.Text = list[i].Kanji;
            }

            //HV
            if (!string.IsNullOrEmpty(list[i].VN_Chinese))
            {
                lblHV.Text = list[i].VN_Chinese;
                //lblKanji.Visible = true;
            }

            //VIETNAM
            if (!string.IsNullOrEmpty(list[i].Vietnamese))
            {
               lblVN.Text = list[i].Vietnamese;
               //lblVN.Visible = true;
            }

            if (i < list.Count - 1)
            {
                i++;
            }
            else {
                i = 0;
            }
        }

        private void Display_Load(object sender, EventArgs e)
        {
            //load list
            list = GetList();
        }

        private void kanjiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isKanji)
            {
                lblKanji.Visible = true;
                isKanji = false;
                kanjiToolStripMenuItem.Checked = true;
            }
            else {
                lblKanji.Visible = false;
                isKanji = true;
                kanjiToolStripMenuItem.Checked = false;
            }
        }

        private void lblHira_MouseHover(object sender, EventArgs e)
        {
            //Set random location;
            Random random = new Random();
            int randomNumber = random.Next(1, 4);

            //Position.EnumPos pos = Enum.ToObject(typeof(Position.EnumPos), randomNumber);
            Position.EnumPos pos = (Position.EnumPos)randomNumber;

            Position.SetPosition(this, pos);
        }

        private void vietnameseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isVietnamese)
            {
                lblVN.Visible = true;
                isVietnamese = false;
                vietnameseToolStripMenuItem.Checked = true;
            }
            else
            {
                lblVN.Visible = false;
                isVietnamese = true;
                vietnameseToolStripMenuItem.Checked = false;
            }
        }

        private void SecondSelect(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;

            //set second
            timerChangeText.Interval =int.Parse(item.Text.TrimEnd('s'))*1000;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
